package com.shuyun.ccms.encrypt.data.impl;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.sql.Connection;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.shuyun.ccms.encrypt.common.DataLoad;
import com.shuyun.ccms.encrypt.data.EncryptProcessor;
import com.shuyun.ccms.encrypt.util.ConnectionUtil;
import com.shuyun.ccms.encrypt.util.PropertyUtil;

public class EncryptDashboardContactProcessor extends	EncryptProcessor {
	private Logger logger = LoggerFactory.getLogger(EncryptDashboardContactProcessor.class);
	private Connection conn;

	@Override
	public void execute() throws Exception {
		// TODO Auto-generated method stub
		BufferedReader br = null;
		logger.info("加密客户手机号码开始.");
		long start = System.currentTimeMillis();
		int count = 1;
		String tenantId = null;

		try{
			String tenantFile = PropertyUtil.getValue("tenantId.path");
			br = new BufferedReader(new InputStreamReader(new FileInputStream(tenantFile)));
			tenantId = br.readLine();
			while(tenantId != null){
				logger.info("第 " + count +  " 个租户: " + tenantId + "加密ccms联系人开始");
				long tenantStart = System.currentTimeMillis();
				//step1: get conn
				conn = ConnectionUtil.newInstance().getTenantConn(tenantId);

				// step2: encrypt dashboard contact
				DataLoad.updateDashCanct(conn);

				long tenantEnd = System.currentTimeMillis();
				logger.info("第 " + count +  " 个租户: " + tenantId + "加密ccms联系人结束,耗时:" + (tenantEnd - tenantStart)/1000 +" s");
				tenantId = br.readLine();
				count++;
			}
			long end = System.currentTimeMillis();
			logger.info("=========== 所有租户加密客户手机号码结束.总耗时: "+ (end-start)/1000 +" s,共加密: " + (count-1) + "个租户");
		}catch(Exception e){
			logger.info("加密客户手机号码失败. tenantId: " + tenantId +",error:" + e.getMessage());
			throw e;
		}finally{
			ConnectionUtil.newInstance().closeConn(conn);
			if(br != null){
				br.close();
			}
		}
	}

}
